
import { _decorator, AnimationClip, Component, Animation, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum';
import State from '../../Base/State';
import { StateMachine, getInitParamsNumber, getInitParamsTrigger } from '../../Base/StateMachine';
import IdleSubMachine from './IdleSubStateMachine';
import IdleSubStateMachine from './IdleSubStateMachine';
import TrunLeftSubStateMachine from './TrunLeftSubStateMachine';
import TurnRightSubStateMachine from './TurnRightSubStateMachine';
import BlockFrontSubStateMachine from './BlockFrontSubStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import BlockTurnLeftSubStateMachine from './BlockTurnLeftSubStateMachine';
import DeathSubStateMachine from './DeathSubStateMachine';
import AttackSubStateMachine from './AttackSubStateMachine';
import AirDeathSubStateMachine from './AirDeathSubStateMachine';
import BlockBackSubStateMachine from './BlockBackSubStateMachine';
import BlockLeftSubStateMachine from './BlockLeftSubStateMachine';
import BlockRightSubStateMachine from './BlockRightSubStateMachine';
import BlockTurnRightSubStateMachine from './BlockTurnRightSubStateMachine';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerState
 * DateTime = Wed Jun 28 2023 09:28:04 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = PlayerState.ts
 * FileBasenameNoExtension = PlayerState
 * URL = db://assets/Script/Player/PlayerState.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

//状态机实现



@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {


    //开始初始化他们了
    async init() {

        this.animationsComponent = this.addComponent(Animation) //拆
        // this.animationsComponent.playOnLoad=true

        this.initParams() //初始参数
        this.initStateMachine() //初始状态机
        this.initAnimationEvent()


        await Promise.all(this.waitingList)
    }


    //动画事件  吧动画变为ido
    initAnimationEvent() {
        this.animationsComponent.on(Animation.EventType.FINISHED, () => {
            const name = this.animationsComponent.defaultClip.name
            const whiteList = ['block', 'turn', 'attack']
            if (whiteList.some(v => name.includes(v))) {
                this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
                // this.setParams(PARAMS_NAME_ENUM.IDLE, true)
            }
        })
    }


    initParams() {  //添加参数默认值？
        this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTrigger())


        this.params.set(PARAMS_NAME_ENUM.TURNLEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.TURNRIGHT, getInitParamsTrigger())

        this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber())


        this.params.set(PARAMS_NAME_ENUM.BLOCKFRONT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCKBACK, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCKLEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCKRIGHT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.BLOCKTURNRIGHT, getInitParamsTrigger())

        this.params.set(PARAMS_NAME_ENUM.DEATH, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.ATTACK, getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.AIRDEATH, getInitParamsTrigger())


        //DeathSubStateMachine

        //BLOCKRONT

        //AirDeathSubStateMachine
    }


    initStateMachine() {
        // //定义idle动画
        // //new一个state?  返回什么？
        // this.stateMachines.set(PARAMS_NAME_ENUM.IDLE,new State(this,"texture/player/idle/top",AnimationClip.WrapMode.Loop))

        //定义turnleft动画
        // this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT,new State(this,"texture/player/turnleft/top"))


        this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT, new TrunLeftSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.TURNRIGHT, new TurnRightSubStateMachine(this))



        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKFRONT, new BlockFrontSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKBACK, new BlockBackSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKLEFT, new BlockLeftSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKRIGHT, new BlockRightSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, new BlockTurnLeftSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKTURNRIGHT, new BlockTurnRightSubStateMachine(this))
        // this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKFRONT, new BlockFrontSubStateMachine(this))
        // this.stateMachines.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, new BlockTurnLeftSubStateMachine(this))


        this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateMachine(this))


        //AttackSubStateMachine
        this.stateMachines.set(PARAMS_NAME_ENUM.ATTACK, new AttackSubStateMachine(this))

        this.stateMachines.set(PARAMS_NAME_ENUM.AIRDEATH, new AirDeathSubStateMachine(this))


        //定义两个状态之间的transition，也就是过渡状态---条件的判断

    }


    //判断当前状态
    run() {
        //判断当前的状态
        switch (this.currentState) {
            case this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKFRONT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKBACK):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKRIGHT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKLEFT):

            case this.stateMachines.get(PARAMS_NAME_ENUM.TURNRIGHT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
            case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):
            case this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK):

            case this.stateMachines.get(PARAMS_NAME_ENUM.AIRDEATH):
                if (this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT)


                } else if (this.params.get(PARAMS_NAME_ENUM.AIRDEATH).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.AIRDEATH)


                } else if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK)


                } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
                    //改变当前状态
                    console.log("DEATH")
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)


                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT)


                }else if (this.params.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKTURNRIGHT)


                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCKFRONT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKFRONT)


                }else if (this.params.get(PARAMS_NAME_ENUM.BLOCKBACK).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKBACK)


                }else if (this.params.get(PARAMS_NAME_ENUM.BLOCKLEFT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKLEFT)


                }else if (this.params.get(PARAMS_NAME_ENUM.BLOCKRIGHT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCKRIGHT)


                } else if (this.params.get(PARAMS_NAME_ENUM.TURNRIGHT).value) {
                    //改变当前状态
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNRIGHT)


                } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
                    this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
                } else {
                    this.currentState = this.currentState
                }
                break;
            default: this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)

        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
